﻿using System;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;

namespace YJKFamilyLibDataManager
{
	public class SqliteFamilyImgTable : IFamilyImgTable
	{
		public SqliteFamilyImgTable(SqliteDatabase db)
		{
			this.mDatabase = db;
		}

		public bool AddLargeImg(DbFamily family, Image img, DbTransaction tr)
		{
			byte[] array = null;
			if (img != null)
			{
				MemoryStream memoryStream = new MemoryStream();
				img.Save(memoryStream, ImageFormat.Jpeg);
				array = memoryStream.ToArray();
			}
			using (DbCommand dbCommand = this.mDatabase.Connection.CreateCommand())
			{
				dbCommand.Transaction = tr;
				dbCommand.CommandText = string.Format("insert into FamilyImgTable (FamilyGUID,LargeImg) values('{0}',{1})", family.Guid.ToString(), "@img");
				object value = this.mDatabase.Sqlite.CreateInstance(SqliteDatabase.SQLiteParameter, true, BindingFlags.CreateInstance, null, new object[]
				{
					"@img",
					array
				}, null, new object[0]);
				dbCommand.Parameters.Add(value);
				dbCommand.ExecuteNonQuery();
			}
			return true;
		}

		public bool Add2DImg(DbFamily family, Image img, DbTransaction tr)
		{
			byte[] array = null;
			if (img != null)
			{
				MemoryStream memoryStream = new MemoryStream();
				img.Save(memoryStream, ImageFormat.Jpeg);
				array = memoryStream.ToArray();
			}
			using (DbCommand dbCommand = this.mDatabase.Connection.CreateCommand())
			{
				dbCommand.Transaction = tr;
				dbCommand.CommandText = string.Format("insert into FamilyImgTable (FamilyGUID,TwoDImg) values('{0}',{1})", family.Guid.ToString(), "@img");
				object value = this.mDatabase.Sqlite.CreateInstance(SqliteDatabase.SQLiteParameter, true, BindingFlags.CreateInstance, null, new object[]
				{
					"@img",
					array
				}, null, new object[0]);
				dbCommand.Parameters.Add(value);
				dbCommand.ExecuteNonQuery();
			}
			return true;
		}

		public bool Add(DbFamily family, Image largeImg, Image twoDImg, DbTransaction tr)
		{
			byte[] array = null;
			if (largeImg != null)
			{
				MemoryStream memoryStream = new MemoryStream();
				largeImg.Save(memoryStream, ImageFormat.Jpeg);
				array = memoryStream.ToArray();
			}
			byte[] array2 = null;
			if (twoDImg != null)
			{
				MemoryStream memoryStream2 = new MemoryStream();
				twoDImg.Save(memoryStream2, ImageFormat.Jpeg);
				array2 = memoryStream2.ToArray();
			}
			using (DbCommand dbCommand = this.mDatabase.Connection.CreateCommand())
			{
				dbCommand.Transaction = tr;
				dbCommand.CommandText = string.Format("insert into FamilyImgTable (FamilyGUID,LargeImg,TwoDImg) values('{0}',{1},{2})", family.Guid.ToString(), "@largeImg", "@twoDImg");
				object value = this.mDatabase.Sqlite.CreateInstance(SqliteDatabase.SQLiteParameter, true, BindingFlags.CreateInstance, null, new object[]
				{
					"@largeImg",
					array
				}, null, new object[0]);
				dbCommand.Parameters.Add(value);
				object value2 = this.mDatabase.Sqlite.CreateInstance(SqliteDatabase.SQLiteParameter, true, BindingFlags.CreateInstance, null, new object[]
				{
					"@twoDImg",
					array2
				}, null, new object[0]);
				dbCommand.Parameters.Add(value2);
				dbCommand.ExecuteNonQuery();
			}
			return true;
		}

		public Image GetLargeImg(DbFamily Family)
		{
			Image result = null;
			DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select [LargeImg] from [FamilyImgTable] where [FamilyGUID]='{0}'", Family.Guid.ToString()));
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				try
				{
					byte[] buffer = (byte[])dataTable.Rows[0]["LargeImg"];
					result = new Bitmap(new MemoryStream(buffer));
				}
				catch
				{
					return null;
				}
				return result;
			}
			return result;
		}

		public Image Get2DImg(DbFamily Family)
		{
			Image result = null;
			DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select [TwoDImg] from [FamilyImgTable] where [FamilyGUID]='{0}'", Family.Guid.ToString()));
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				try
				{
					byte[] buffer = (byte[])dataTable.Rows[0]["TwoDImg"];
					result = new Bitmap(new MemoryStream(buffer));
				}
				catch
				{
					return null;
				}
				return result;
			}
			return result;
		}

		public bool UpdateLargeImg(DbFamily family, Image img)
		{
			bool result;
			using (DbCommand dbCommand = this.mDatabase.Connection.CreateCommand())
			{
				dbCommand.CommandText = string.Format("update [FamilyImgTable] set [LargeImg]={0} where [FamilyGUID]='{1}'", "@img", family.Guid.ToString());
				byte[] array = null;
				if (family.Image != null)
				{
					MemoryStream memoryStream = new MemoryStream();
					img.Save(memoryStream, ImageFormat.Jpeg);
					array = memoryStream.ToArray();
				}
				object value = this.mDatabase.Sqlite.CreateInstance(SqliteDatabase.SQLiteParameter, true, BindingFlags.CreateInstance, null, new object[]
				{
					"@img",
					array
				}, null, new object[0]);
				dbCommand.Parameters.Add(value);
				if (dbCommand.ExecuteNonQuery() != -1)
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			return result;
		}

		public bool UpdateSmallImg(DbFamily family, Image img)
		{
			bool result;
			using (DbCommand dbCommand = this.mDatabase.Connection.CreateCommand())
			{
				dbCommand.CommandText = string.Format("update [FamilyImgTable] set [SmallImg]={0} where [FamilyGUID]='{1}'", "@img", family.Guid.ToString());
				byte[] array = null;
				if (family.Image != null)
				{
					MemoryStream memoryStream = new MemoryStream();
					img.Save(memoryStream, ImageFormat.Jpeg);
					array = memoryStream.ToArray();
				}
				object value = this.mDatabase.Sqlite.CreateInstance(SqliteDatabase.SQLiteParameter, true, BindingFlags.CreateInstance, null, new object[]
				{
					"@img",
					array
				}, null, new object[0]);
				dbCommand.Parameters.Add(value);
				if (dbCommand.ExecuteNonQuery() != -1)
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			return result;
		}

		public bool Update2DImg(DbFamily family, Image img)
		{
			bool result;
			using (DbCommand dbCommand = this.mDatabase.Connection.CreateCommand())
			{
				dbCommand.CommandText = string.Format("update [FamilyImgTable] set [TwoDImg]={0} where [FamilyGUID]='{1}'", "@img", family.Guid.ToString());
				byte[] array = null;
				if (family.Image != null)
				{
					MemoryStream memoryStream = new MemoryStream();
					img.Save(memoryStream, ImageFormat.Jpeg);
					array = memoryStream.ToArray();
				}
				object value = this.mDatabase.Sqlite.CreateInstance(SqliteDatabase.SQLiteParameter, true, BindingFlags.CreateInstance, null, new object[]
				{
					"@img",
					array
				}, null, new object[0]);
				dbCommand.Parameters.Add(value);
				if (dbCommand.ExecuteNonQuery() != -1)
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			return result;
		}

		public bool Remove(DbFamily family, DbTransaction tr)
		{
			bool result;
			try
			{
				this.mDatabase.ExecuteCommand("delete from [FamilyImgTable] where [FamilyGUID]='" + family.Guid.ToString() + "'", tr);
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private SqliteDatabase mDatabase;
	}
}
